package org.test.transaction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.test.aspect.Proxy;
import org.test.aspect.ProxyChain;

import java.lang.reflect.Method;

/**
 * 事务代理
 * Created by Hy on 2018/7/12.
 */
public class TransactionProxy implements Proxy{
    private static final Logger logger = LoggerFactory.getLogger(TransactionProxy.class);
    private static final ThreadLocal<Boolean> FLAG_HOLDER = new ThreadLocal<Boolean>(){
        @Override
        protected Boolean initialValue() {
            return false;
        }
    };
    public Object doProxy(ProxyChain proxyChain) throws Throwable {
        Object result;
        boolean flag = FLAG_HOLDER.get();
        Method method = proxyChain.getTargetMethod();
        if(!flag && method.isAnnotationPresent(Transaction.class)){
            try {
                DatabaseHelper.beginTransaction();
                logger.debug("begin transaction");
                result = proxyChain.doProxyChain();
                DatabaseHelper.commitTransaction();
                logger.debug("commit transaction");
            }catch(Exception e){
                DatabaseHelper.rollbackTransaction();
                logger.debug("rollback transaction");
                throw e;
            }finally{
                FLAG_HOLDER.remove();
            }
        }else{
            result = proxyChain.doProxyChain();
        }
        return result;
    }
}
